# 作业：登录注册的案例
# main函数要有，用户自己选择要做的功能，根据选择调用不同的函数
# 用户注册的信息需要使用一个文件存储，登录需要判断用户是否存在，密码是否正确
# 注册的时候，需要发送邮件
def zhuce():
    while True:
        name=input('请输入您的姓名：')
        pwd=input('请输入您的密码：')
        email=input('请输入您的邮箱：')
        infos = f"{name},{pwd},{email}\n"
        f = open('E:\\projects\\PycharmProjects\\bigdata33\\data\\users.csv', mode='a', encoding='UTF-8')
        f.write(infos)
        f.close()
        print(send_email('1531472317@qq.com','恭喜您注册成功'))
        break
def send_email(msg_to,send_info):
    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    msg_from = '1531472317@qq.com'
    passwd = 'whoupizggkfchaif'
    msg_to = '1531472317@qq.com'
    subject = "欢迎您注册登录账户"
    content = '您的注册账户的验证码是123456'
    msg = MIMEText(content)
    msg['Subject'] = subject
    msg['From'] = msg_from
    s = smtplib.SMTP_SSL("smtp.qq.com", 465)
    s.login(msg_from, passwd)
    s.sendmail(msg_from, msg_to, msg.as_string())
    print('成功')
def denglu():
    res1=''
    while True:
        name=input('请输入您的姓名：')
        pwd=input('请输入您的密码：')
        f = open('E:\\projects\\PycharmProjects\\bigdata33\\data\\users.csv', mode='r', encoding='UTF-8')
        res1 = f.readlines()
        for i in res1:
            if i == name:
                print('此用户存在,登录成功')
            else:
                print('此用户不存在,请先注册')
def main():
    while True:
        print("请选择功能:")
        print("1.注册")
        print("2.登录")
        print("3.退出")
        xuanze=input('请输入选项：')
        if xuanze == "1":
            zhuce()
        elif xuanze  == "2":
            denglu()
        elif xuanze  == "3":
            print("退出程序。")
            break
        else:
            print("没有此选项，请重新选择")
main()